﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.Repository;
using System.Web.ModelBinding;
using System.Data.Entity;


namespace RMS.Pages.Admin
{
    /* Code-Behind for Retreats web form allows inserting, updating, and deleting retreats from the database. */
     
    public partial class AddRetreat : System.Web.UI.Page {
        private Repository repo = new Repository();
        
        protected void Page_Load(object sender, EventArgs e) {
            
            inputSuccess.Visible = false;
                       
            if (IsPostBack)
            {
                                               
                       
            }
           
        }

        // The GetRetreatss method reads our data objects.
        public IEnumerable<Retreat> GetRetreats() {
            return repo.Retreats
            .OrderBy(p => p.Ret_Title);
        }

        // Method for updating a Retreat. 
        public void UpdateRetreat(int Ret_ID) {
            Retreat myRetreat = repo.Retreats
            .Where(p => p.Ret_ID == Ret_ID).FirstOrDefault();
            if (myRetreat != null && TryUpdateModel(myRetreat,
            new FormValueProvider(ModelBindingExecutionContext)))
            {
                repo.SaveRetreat(myRetreat);
            }
        }

        // Method for deleting a Retreat. 
        public void DeleteRetreat(int Ret_ID)
        {
            Retreat myRetreat = repo.Retreats
            .Where(p => p.Ret_ID == Ret_ID).FirstOrDefault();
            if (myRetreat != null)
            {
                repo.DeleteRetreat(myRetreat);
            }
        }

        // Method for inserting a Retreat.
        public void InsertRetreat() {
            Retreat myRetreat = new Retreat();
            if (TryUpdateModel(myRetreat,
            new FormValueProvider(ModelBindingExecutionContext)))
            {
                repo.SaveRetreat(myRetreat);
            }
            inputSuccess.Visible = true;
        }

    }
}